<?php

namespace App\Http\Controllers\Api\V1;
use App\Models\Account;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Api\V1\Resources\AccountResource;

class AccountsController extends Controller
{
	public function index()
	{

		$source = request()->source;

		if($source==1){

			$startTime = Carbon::now()->startOfMonth()->toDateTimeString();
			$endTime = Carbon::now()->startOfMonth()->addMonth()->toDateTimeString();


		}else{
			$startTime = Carbon::now()->startOfMonth()->subMonth()->toDateTimeString();
			$endTime = Carbon::now()->startOfMonth()->toDateTimeString();

		}
		

		$joinSql =  "(select sum(money) as money,id as act_id  from accounts where created_at >= '{$startTime}' and created_at<='{$endTime}' and remark='system_transfer_coins' group by user_id  ) AS `r` ";
		$accounts = Account::query()
			->join(DB::raw($joinSql),'r.act_id','=','accounts.id')
			->select("id","user_id",'r.money')
			->orderByDesc("r.money")
			->paginate();
		/*$accounts = Account::query()
				->select("id","user_id",DB::raw('SUM(money) as money'))
				->where("created_at",'>',$startTime)
				->where("created_at","<",$endTime)
				->where("remark","system_transfer_coins")
				->groupBy("user_id")->paginate();*/
	
				
		return AccountResource::collection($accounts);
	}


}
